<?php

namespace Home\Controller;
use Common\Service\VideoService;
/**
 * 前台视频管理   AccessUserController
 */
class MyVideoController extends BaseUserController 
{

	protected $text_map;

	protected function _initialize(){
		parent::_initialize();
		C('TOKEN_ON', true);
		$games = A('Api/Game', 'Event')->lists();
		$service = D('Video', 'Service');
		foreach ($games as $key => $value) {
			$this->text_map['game_id'][$value['id']] = $value['name'];
		}
 
		$this->text_map['recommend'] = array(
			$service::RECOMMEND_NO   => '否',
			$service::RECOMMEND_YES  => '推荐',
		);
		$this->text_map['status'] = array(
			$service::STATUS_PEDDING  => '审核中',
			$service::STATUS_PASS     => '审核通过',
		);
		$this->assign('options', $this->text_map);
	}

	public function index()
	{
		#TODO 筛选 :   游戏 - 赛事
		$filter_map = array('id','title','thumb','uri','time', 'detail','create_time','game_id','game','uid','user','statis','tags');
		$service = D('Video', 'Service');
		$map['uid'] 	= UID;
		$map['status']  = array('egt', 0);
		$lists = $this->lists($service, $map, 'create desc');
		int_to_string($lists, $this->text_map);
		$this->assign('lists', $lists);
		$this->display();
	}


	public function choose_game()
	{
		$games = A('Api/Game', 'Event')->lists(array('status' => 1));

		//add by susy<susy.liang@ilongyuan.com.cn> 游戏封面图全地址
		if($games){
			foreach ($games as $key => &$value) {
				uri_format($value['cover']);
			}
		}
		$this->games = $games;
		
		$this->display();
	}


	// 投稿
	public function submit()
	{
		$game_id = I('game_id', 0, 'intval');
		$game = A('Api/Game', 'Event')->info($game_id);
		if (!$game)
		{
			$this->redirect('choose_game');
		}
		$this->game = $game;
		$this->display();
	}

	// 投稿操作
	public function do_submit()
	{
		if (I('hash') == 'no')
		{
			C('TOKEN_ON', false);
		}
		$Event = A('Api/Video', 'Event');
		$data  = I();
		$_REQUEST['full_title'] = $data['full_title'] = $data['title'];
		$data['status'] = C('AUTO_VIDEO_OPEN',null, false) ? VideoService::STATUS_PASS : VideoService::STATUS_PEDDING;
		#TODO 获取用户名
		$data['uid']    = UID; 
		I('race_id') && $data['race_id'] = I('race_id', null, 'intval');
		I('match_id') && $data['match_id'] = I('match_id', 0, 'intval');
		$res = $Event->add($data);
		if (!$res)
		{
			$this->error($Event->getError());
		}

		$this->success('投稿成功', U('index') );
	}

	// 更新投稿
	public function update()
	{

	}

	




}